package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.OrderBillDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjzuji.rental.mapper.OrderBillMapper;
import com.cjzuji.rental.mapper.OrderContractMapper;
import com.cjzuji.rental.pojo.OrderBill;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class OrderBillDaoImopl implements OrderBillDao {
    private static final Logger logger = LoggerFactory.getLogger(OrderBillDaoImopl.class);
    @Autowired
    OrderBillMapper orderBillMapper;

    @Override
    @Transactional
    public  void save(List<Map<String,Object>> data, Long orderID, String orderNo){
        //System.out.println("循环遍历："+data.toString());
        List<OrderBill>  billList = new ArrayList<>();
        for (Map<String, Object> item : data){
            //logger.info("账单信息添加开始{}", item.toString());
            String realRepayTime = item.containsKey("realRepayTime") ? item.get("realRepayTime").toString() : "";
            OrderBill orderBill = OrderBill.builder()
                    .orderId(orderID)
                    .ordersId(orderNo)
                    .realPayTime(realRepayTime) // 支付时间
                    .term((int) item.get("term"))   //租期
                    .realPayCapital((Double)item.get("actRepayCapital"))  //实付金额  actRepayCapital
                    .capital((Double) item.get("totalMonthRepayAmt"))      //应还金额  totalMonthRepayAmt
                    .monthRepayment((Double)item.get("capital"))  //月租金  capital
                    .zfbDiscountAmt((Double) item.get("platformDiscountAmt"))  //支付宝优惠金额
                    .ovdueBuyoutAmt((Double) item.get("termSurplusBuyoutAmt"))     //逾期买断金
                    .writeOffAmt((Double) item.get("discountAmt"))      //优惠金额
                    .reduceAmt((Double) item.get("reduceAmt"))      //减免金额
                    .overdueAmount((Double) item.get("realOverdueFine"))       //逾期违约金
                    .repayDateFmt(item.get("repayDateFmt").toString())  //还款日
                    .realPayTradeId(item.get("thirdPartOrderNo").toString()) // 商家交易号
                    .repayStatusStr(item.get("repayStatusStr").toString())  //还款状态
                    .realRepayTime(realRepayTime)   //还款时间
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            billList.add(orderBill);
        }

        try {
           // System.out.println("批量保存：~~~~~~~~~~"+ billList.toString());
            orderBillMapper.insertBatchSomeColumn(billList);
        }catch (Exception e){
            logger.info("账单信息添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }
    @Override
    public List<OrderBill> getOrderBillList(Long orderId) {
        return orderBillMapper.getOrderBillList(orderId);
    }
}
